﻿
//依赖项：
//mfcvcr100.dll;kernel32.dll



#include "..\common\serialport.h"

//作为已知类型
struct shutInterface
{
	bool (*openCom)(int portnum);
	void (*closeCom)();

	void (*openShut)() ;
	void (*closeShut)() ;
	void (*openShutInterval)(int ms) ;
};

static serialPort com;



//dll入口
BOOL APIENTRY DllMain( HANDLE hModule,
	DWORD  ul_reason_for_call,
	LPVOID lpReserved
	)
{
	switch( ul_reason_for_call )
	{
	case DLL_PROCESS_ATTACH:break;
	case DLL_THREAD_ATTACH:break;
	case DLL_THREAD_DETACH:break;
	case DLL_PROCESS_DETACH:
		com.CloseCom();
	}
	return TRUE;
}


//私有
bool send(const char* cmd)
{
	if(!com.WriteCom(cmd))
	{
		MessageBoxW(0,L"发送失败",L"shut串口",0);
		return false;
	}
	return true;
}
bool read(char* dat)
{
	if(!com.ReadCom(dat))
	{
		MessageBoxW(0,L"读取失败",L"shut串口",0);
		return false;
	}
	return true;
}

///接口
bool openCom(int portnum)
{
	return com.OpenCom(portnum);
}

void closeCom()
{
	com.CloseCom();
}

void openShut()
{
	send("@\r");
	//Sleep(500);
	//DWORD dwEventMask=0; //发生的事件；
	//OVERLAPPED ov1 = { 0, 0, 0, 0, 0 } ;
	//ov1.hEvent=CreateEventA(NULL,FALSE,FALSE,"ov1");//自动复位
	//WaitCommEvent(com.hComm,&dwEventMask,&ov1);

}
void closeShut()
{
	if(!send("A\r"))
	{
		return ;
	}
	//DWORD dwEventMask=0; //发生的事件；
	//OVERLAPPED ov1 = { 0, 0, 0, 0, 0 } ;
	//ov1.hEvent=CreateEventA(NULL,FALSE,FALSE,"ov1");//自动复位
	//WaitCommEvent(com.hComm,&dwEventMask,&ov1);
}
void openShutInterval(int ms)
{
	send("@\r");
	Sleep(ms);
	send("A\r");
	Sleep(50);//等待关闭完全,防止拖线
}
//struct shutInterface
//{
//	bool (*openShut)() ;
//	bool (*closeShut)() ;
//	bool (*openShutInterval)(int ms) ;
//};

extern "C" __declspec(dllexport) void setupShutEngine(shutInterface& dest)
{
		//com.SetMask(EV_RXCHAR);
	dest.openCom=openCom;
	dest.closeCom=closeCom;

	dest.openShut=openShut;
	dest.closeShut=closeShut;
	dest.openShutInterval=openShutInterval;
}


